<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\material;

use app\model\mongo\MongoPicModel;
use app\model\PicModel;
use app\service\material\PicService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

/**
 * 系统图片服务.
 */
final class PicMaterialService extends MaterialService implements MaterialInterface
{
    /**
     * 获取列表数据.
     *
     * @param int $page 页数
     * @param int $limit 分页
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(int $page = 1, int $limit = 200)
    {
        return (new PicModel())
            ->where('is_mark', PicModel::MARK_NO)
            ->limit(($page - 1) * $limit, $limit)
            ->select();
    }

    /**
     * 设置视频信息.
     *
     * @param Model $model 图片模型
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function setData(Model $model): bool
    {
        if (! $model instanceof PicModel) {
            return self::setError('未知图片模型');
        }

        if (! $model->getKey()) {
            return self::setError('未知视频模型');
        }

        $file = $this->getFile(intval($model->getAttr('file_id')));
        if (! $file->getKey()) {
            return self::setError('未知文件模型');
        }

        $creativeStaffs = PicService::getPicCreativeStaff($model);
        if ($creativeStaffs === false) {
            return false;
        }

        $operationId = PicService::getPicTaskOperation($model);
        if ($operationId === false) {
            return false;
        }

        $areaId = PicService::getPicDepartmentArea($model);
        if ($areaId === false) {
            return false;
        }

        $obj = (new MongoPicModel())
            ->where('signature', $model->getAttr('signature'))
            ->field(true)
            ->find();
        if (! $obj instanceof MongoPicModel) {
            $obj = new MongoPicModel();
        }

        $obj->setAttrs([
            'pic_id' => $model->getKey(),
            'company_id' => $model->getAttr('company_id'),
            'client_id' => $model->getAttr('client_id'),
            'department_id' => $model->getAttr('department_id'),
            'area_id' => $areaId,
            'product_id' => $model->getAttr('product_id'),
            'name' => $model->getAttr('name'),
            'code_num' => $model->getAttr('code_num'),
            'file_id' => $model->getAttr('file_id'),
            'task_id' => $model->getAttr('task_id'),
            'subtask_id' => $model->getAttr('subtask_id'),
            'signature' => $model->getAttr('signature'),
            'version' => $model->getAttr('version'),
            'parent_id' => $model->getAttr('parent_id'),
            'is_abandon' => $model->getAttr('is_abandon'), // 是否弃用
            'pic_type' => $model->getAttr('pic_type'), // 图片类型
            'source' => $model->getAttr('task_id') > 0 ? MongoPicModel::SOURCE_ZHICHUANG : MongoPicModel::SOURCE_LOCAL,
            'creative_staff_id' => $creativeStaffs,
            'operation_staff_id' => $operationId,
            'creative_team_id' => PicService::getPicTaskTeam($model),
            'is_use' => MongoPicModel::USE_DISABLE,
            'create_time' => $file->getAttr('create_time'),
            'update_time' => $model->getAttr('update_time'),
        ]);
        return $obj->save();
    }

    /**
     * 根据签名获取图片模型.
     *
     * @param string $signature 签名=
     * @return array|mixed|Model|MongoPicModel
     */
    public static function getPicBySignature(string $signature = '')
    {
        return (new MongoPicModel())->where('signature', $signature)->findOrEmpty();
    }
}
